<?php

namespace Home\Model;

use Think\Model;

class UserActivevalModel extends CommonModel
{
    public function inc($user_id, $channel = '')
    {
        return false;
        if($channel == ''){
            return false;
        }

        $UserActiveval = new UserActivevalModel;
        $user = new UserModel;
        $UserActivevalRecords = new UserActivevalRecordsModel;
        $ConfigSystem = new ConfigSystemModel;

        // 获取上限和增值
        $config = $ConfigSystem->index('useractiveval');

        $date = date('Y-m-d');
        $today_start = strtotime($date . '00:00:00');
        $today_end = strtotime($date . '23:59:59');

        $today = $UserActivevalRecords->where(['user_id'=>$user_id, 'channel'=>$channel, 'time'=>array('between',array($today_start,$today_end))])->sum('value');

        if($config[$channel . '_limit'] <= $today){
            return false;
        }
        $value = $config[$channel . '_once'];

        // 是否存在活跃度记录
        $Activeval = $UserActiveval->where(["user_id" => $user_id])->find();
        if($Activeval){
            $UserActiveval->where(['user_id'=>$user_id])->setInc('today', $value);
        }else{
            $UserActiveval->add([
                "user_id" => $user_id,
                "today" => $value,
            ]);
        }

        // 写记录表
        $UserActivevalRecords->add(['user_id'=>$user_id, 'channel'=>$channel, 'value'=>$value, 'time'=>time()]);
        // 写用户活跃度
        $user->where(['id'=>$user_id])->setInc('activeval', $value);

    }
}
